package com.cyc.quick.controller;


import com.cyc.quick.pojo.entity.SysUser;
import com.cyc.quick.pojo.vo.UserInfoVO;
import com.cyc.quick.pojo.vo.UserReq;
import com.cyc.quick.service.ISysUserService;
import com.cyc.quick.exception.ProjectException;
import com.cyc.quick.pojo.vo.SysUserAddVO;
import com.cyc.quick.util.ApiResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chenyunchang
 * @since 2021-01-19
 */
@RestController
@RequestMapping("/sysUser")
@Api(tags = "用户相关接口")
@Slf4j
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    /**
     * @param sysUserAddVO 添加用户对象
     * @return com.weiyou.user.util.ApiResult<java.lang.String>
     * @author chenyunchang
     * @description 用户注册
     * @date 2021-01-20 09:25:23
     */
    @ApiOperation("注册")
    @PostMapping("/register")
    public ApiResult<String> register(@Validated @RequestBody SysUserAddVO sysUserAddVO, BindingResult result, HttpServletRequest request) {
        if (result.hasErrors()) {
            //将错误拼接成字符串
            String msg = result.getAllErrors().stream().map(ObjectError::getDefaultMessage).collect(Collectors.joining(","));
            throw new ProjectException("400", msg);
        }
        sysUserService.register(sysUserAddVO, request);
        return ApiResult.success();
    }

    @ApiOperation("分页查询")
    @GetMapping("/findPage")
    public ApiResult<PageInfo<UserInfoVO>> page(UserReq userReq) {
        PageInfo<UserInfoVO> pageInfo = sysUserService.findPage(userReq);
        return ApiResult.success(pageInfo);
    }

}

